/*
 * A121_app.c
 *
 *  Created on: 2023年4月25日
 *      Author: Administrator
 */
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "user_app.h"
#include "main.h"
#include "flash.h"

#include "main.h"

extern uint8_t syt_mode;
extern uint8_t USART1_RX_BUF[UART_BUF_NUB];

#define BASE_ADDR         0X0803e000  //激活标志位
#define SAVE_SIZE_BUF  2048
#define SAVE_SIZE  SAVE_SIZE_BUF
uint8_t save_buf[SAVE_SIZE_BUF];//

extern A121_presence_config_t  A121_presence_config;
extern A121_distance_config_t  A121_distance_config;
extern acc_cal_result_save_t   acc_cal_result_save;

extern A121_breath_config_t A121_breath_config;
extern uint16_t USART2_RX_STA;
extern UART_HandleTypeDef huart2;
extern UART_HandleTypeDef huart1;


void Reset_syt(void)
{
	__set_FAULTMASK(1);
	NVIC_SystemReset();
}

union sw_uint16_t//共同体
{
    uint8_t    c[4];
    int16_t    u16;
};

union sw_float_t//共同体
{
    uint8_t    c[4];
    float     ufloat;
};
void SW_sendfloat_format(const float_t data1,const float_t data2,const float_t data3)
{
	uint8_t i;
	uint8_t temp=2;
	uint8_t s_buf[20];
	union sw_float_t sw_float;

	sw_float.ufloat=data1;
	s_buf[temp++]=sw_float.c[0];
	s_buf[temp++]=sw_float.c[1];
	s_buf[temp++]=sw_float.c[2];
	s_buf[temp++]=sw_float.c[3];

	sw_float.ufloat=data2;
	s_buf[temp++]=sw_float.c[0];
	s_buf[temp++]=sw_float.c[1];
	s_buf[temp++]=sw_float.c[2];
	s_buf[temp++]=sw_float.c[3];

	sw_float.ufloat=data3;
	s_buf[temp++]=sw_float.c[0];
	s_buf[temp++]=sw_float.c[1];
	s_buf[temp++]=sw_float.c[2];
	s_buf[temp++]=sw_float.c[3];

	s_buf[0]=0x03;s_buf[1]=0xFC;
	s_buf[temp++]=0xFC;s_buf[temp++]=0x03;
	HAL_UART_Transmit(&huart1,s_buf,temp,200);
}

void SW_send16_format(uint16_t data1,uint16_t data2,uint16_t data3,uint16_t data4)
{
	uint8_t i;
	uint8_t s_buf[20];
	union sw_uint16_t scy_uint16;

	scy_uint16.u16=data1;
	s_buf[2]=scy_uint16.c[0];
	s_buf[3]=scy_uint16.c[1];

	scy_uint16.u16=data2;
	s_buf[4]=scy_uint16.c[0];
	s_buf[5]=scy_uint16.c[1];

	scy_uint16.u16=data3;
	s_buf[6]=scy_uint16.c[0];
	s_buf[7]=scy_uint16.c[1];

	scy_uint16.u16=data4;
	s_buf[8]=scy_uint16.c[0];
	s_buf[9]=scy_uint16.c[1];

	s_buf[0]=0x03;s_buf[1]=0xFC;
	s_buf[10]=0xFC;s_buf[11]=0x03;
	HAL_UART_Transmit(&huart1,s_buf,12,200);
}


void SW_send16_2format(uint16_t data1,uint16_t data2)
{
	uint8_t i;
	uint8_t s_buf[20];
	union sw_uint16_t scy_uint16;

	scy_uint16.u16=data1;
	s_buf[2]=scy_uint16.c[0];
	s_buf[3]=scy_uint16.c[1];

	scy_uint16.u16=data2;
	s_buf[4]=scy_uint16.c[0];
	s_buf[5]=scy_uint16.c[1];

	s_buf[0]=0x03;s_buf[1]=0xFC;
	s_buf[6]=0xFC;s_buf[7]=0x03;
	HAL_UART_Transmit(&huart1,s_buf,8,200);
}



void  presence_save_data(void)
{
	uint8_t temp=0;

	uint8_t flag=0;
	uint8_t i=0;
	union uf_union  uf;


	if(A121_presence_config.save_flag==1)
	{
		save_buf[temp++]=syt_mode;
		save_buf[temp++]=A121_presence_config.format;
		save_buf[temp++]=A121_presence_config.ID;

		//	presence常规数据
		uf.f=A121_presence_config.start;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}

		uf.f=A121_presence_config.end;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}


		save_buf[temp++]=((A121_presence_config.step_length>>8)&0x00ff);
		save_buf[temp++]=A121_presence_config.step_length&0x00ff;

		save_buf[temp++]=A121_presence_config.profile;

		save_buf[temp++]=A121_presence_config.frame_idle;

		save_buf[temp++]=((A121_presence_config.hwaas>>8)&0x00ff);
		save_buf[temp++]=A121_presence_config.hwaas&0x00ff;

		save_buf[temp++]=((A121_presence_config.sweeps_per_frame>>8)&0x00ff);
		save_buf[temp++]=A121_presence_config.sweeps_per_frame&0x00ff;

		uf.f=A121_presence_config.frame_rate;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}

		save_buf[temp++]=A121_presence_config.en_frame_rate;
		save_buf[temp++]=A121_presence_config.en_reset_filters;
		//	presence算法参数
		//帧内检测
		save_buf[temp++]=A121_presence_config.en_intra_detec_flag;

		uf.f=A121_presence_config.intra_threshold;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}

		uf.f=A121_presence_config.intra_frame_time;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}

		uf.f=A121_presence_config.intra_output_time;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}
		//帧间检测
		save_buf[temp++]=A121_presence_config.en_inter_detec_flag;

		uf.f=A121_presence_config.inter_threshold;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}

		uf.f=A121_presence_config.inter_deviation;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}

		uf.f=A121_presence_config.inter_fast_cutoff;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}

		uf.f=A121_presence_config.inter_slow_cutoff;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}

		uf.f=A121_presence_config.inter_output_time;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}


		uf.f=A121_presence_config.inter_frame_presence_timeout;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}

		save_buf[temp++]=A121_presence_config.en_inter_phase_boost;

		save_buf[temp++]=((A121_presence_config.data_output_time>>8)&0x00ff);
		save_buf[temp++]=A121_presence_config.data_output_time&0x00ff;

		STMFLASH_Write(BASE_ADDR, (uint32_t*)save_buf, SAVE_SIZE);

		A121_presence_config.save_flag=0;
		USART2_RX_STA=0;
//		Reset_syt();//保存参数后复位起效
	}
}

void distance_save_data(void)
{
	uint16_t temp=0;

	uint8_t flag=0;
	uint16_t i=0;
	uint16_t j=0;
	union uf_union  uf;
	union u32_union  u32t;
	union u16_union  u16t;

//	A121_distance_config.save_flag=1;

	if(A121_distance_config.save_flag==1)
	{
		//distance///////////////////////////////////////////////////////////
		save_buf[temp++]=syt_mode;
		save_buf[temp++]=A121_distance_config.format;
		save_buf[temp++]=A121_distance_config.ID;

		uf.f=A121_distance_config.start;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}

		uf.f=A121_distance_config.end;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}


		save_buf[temp++]=((A121_distance_config.step_length>>8)&0x00ff);
		save_buf[temp++]=A121_distance_config.step_length&0x00ff;

		save_buf[temp++]=A121_distance_config.profile;
		save_buf[temp++]=A121_distance_config.reflector_shape;

		save_buf[temp++]=A121_distance_config.peak_sorting;
		save_buf[temp++]=A121_distance_config.threshold_method;

		save_buf[temp++]=((A121_distance_config.num_frames_recorded>>8)&0x00ff);
		save_buf[temp++]=A121_distance_config.num_frames_recorded&0x00ff;

		uf.f=A121_distance_config.threshold_sensitivity;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}
		uf.f=A121_distance_config.signal_quality;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}

		save_buf[temp++]=A121_distance_config.close_cancellation;

/////////////////////////
		save_buf[temp++]=((A121_distance_config.hwaas>>8)&0x00ff);
		save_buf[temp++]=A121_distance_config.hwaas&0x00ff;


		save_buf[temp++]=A121_distance_config.gain;

		save_buf[temp++]=((A121_distance_config.sweeps_per_frame>>8)&0x00ff);
		save_buf[temp++]=A121_distance_config.sweeps_per_frame&0x00ff;


		uf.f=A121_distance_config.sweep_rate;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}

		save_buf[temp++]=A121_distance_config.continuous_sweep_mode;

		save_buf[temp++]=((A121_distance_config.threshold>>8)&0x00ff);
		save_buf[temp++]=A121_distance_config.threshold&0x00ff;

		uf.f=A121_distance_config.frame_rate;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}
		save_buf[temp++]=A121_distance_config.frame_idle;

		save_buf[temp++]=A121_distance_config.sweep_idle;

		save_buf[temp++]=A121_distance_config.prf;

		save_buf[temp++]=A121_distance_config.phase_enhancemen_flag;

		save_buf[temp++]=A121_distance_config.double_buffering_flag;


		save_buf[temp++]=((A121_distance_config.data_output_time>>8)&0x00ff);
		save_buf[temp++]=A121_distance_config.data_output_time&0x00ff;

		save_buf[temp++]=A121_distance_config.average;

		save_buf[temp++]=A121_distance_config.scan_nub;


		//校准值保存
		save_buf[temp++]=acc_cal_result_save.use_cal_caching_flag;
		save_buf[temp++]=acc_cal_result_save.cal_flag;

		u16t.u16=acc_cal_result_save.close_buf_size;
		for(i=0;i<2;i++)
		{
			save_buf[temp++]=u16t.c[i];
		}

		save_buf[temp++]=acc_cal_result_save.temp_buf_size;

		for(i=0;i<1500;i++)
		{
			save_buf[temp++]=acc_cal_result_save.close_buf[i];
		}
		for(j=0;j<50;j++)
		{
			u32t.u32=acc_cal_result_save.temp_buf[j];
			for(i=0;i<4;i++)
			{
				save_buf[temp++]=u32t.c[i];
			}
		}

		STMFLASH_Write(BASE_ADDR, (uint32_t*)save_buf, SAVE_SIZE);

		A121_distance_config.save_flag=0;
		USART2_RX_STA=0;
	}
}

void breath_save_data(void)
{
	uint8_t temp=0;

	uint8_t flag=0;
	uint8_t i=0;
	union uf_union   uf;
	union u16_union  u16;


	if(A121_breath_config.save_flag==1)
	{
		save_buf[temp++]=3;
		save_buf[temp++]=0;
		save_buf[temp++]=1;

		uf.f=A121_breath_config.start;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}

		uf.f=A121_breath_config.end;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}

		u16.u16=A121_breath_config.step_length;
		for(i=0;i<2;i++)
		{
			save_buf[temp++]=u16.c[i];
		}

		u16.u16=A121_breath_config.num_dists_to_analyze;
		for(i=0;i<2;i++)
		{
			save_buf[temp++]=u16.c[i];
		}


		uf.f=A121_breath_config.distance_determination_duration;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}


		save_buf[temp++]=A121_breath_config.use_presence_processor;

		uf.f=A121_breath_config.lowest_breathing_rate;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}

		uf.f=A121_breath_config.highest_breathing_rate;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}

		uf.f=A121_breath_config.time_series_length_s;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}

		uf.f=A121_breath_config.frame_rate;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}


		u16.u16=A121_breath_config.sweeps_per_frame;
		for(i=0;i<2;i++)
		{
			save_buf[temp++]=u16.c[i];
		}
		u16.u16=A121_breath_config.hwaas;
		for(i=0;i<2;i++)
		{
			save_buf[temp++]=u16.c[i];
		}

		save_buf[temp++]=A121_breath_config.profile;

		//////////////////////////////
		uf.f=A121_breath_config.Breath_presence.Intra_detection_threshold;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}
		uf.f=A121_breath_config.Breath_presence.Intra_time_constant;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}
		uf.f=A121_breath_config.Breath_presence.Intra_output_time_constant;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}
		uf.f=A121_breath_config.Breath_presence.Inter_detection_threshold;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}
		uf.f=A121_breath_config.Breath_presence.Inter_fast_cutoff_freq;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}
		uf.f=A121_breath_config.Breath_presence.Inter_slow_cutoff_freq;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}
		uf.f=A121_breath_config.Breath_presence.Inter_time_constant;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}
		uf.f=A121_breath_config.Breath_presence.Inter_output_time_constant;
		for(i=0;i<4;i++)
		{
			save_buf[temp++]=uf.c[i];
		}


		STMFLASH_Write(BASE_ADDR, (uint32_t*)save_buf, SAVE_SIZE);

		A121_breath_config.save_flag=0;
		USART2_RX_STA=0;
	}
}

void A121_app_save_data(void)
{
	if(syt_mode==0)
	{
		distance_save_data();
	}
	else if(syt_mode==1)
	{
		presence_save_data();
	}
	else
	{
		breath_save_data();
	}
}

void distance_read_save_data(void)
{

	uint16_t temp=0;
	uint8_t fcount=0;

	uint16_t i=0;
	uint16_t j=0;
	union uf_union  uf;
	union u32_union  u32t;
	union u16_union  u16t;

	STMFLASH_Read(BASE_ADDR, (uint32_t*)save_buf, SAVE_SIZE);

	syt_mode=save_buf[temp++];
	if(syt_mode>3){syt_mode=0;}

	A121_distance_config.format=save_buf[temp++];
	A121_distance_config.ID=save_buf[temp++];

	fcount=0;
	for(i=0;i<4;i++)
	{
		uf.c[i]=save_buf[temp++];
		if(uf.c[i]==0xff){fcount++;}
	}
	A121_distance_config.start=uf.f;
	if((A121_distance_config.start<0.001)||(A121_distance_config.start>20)||(fcount==4))
	{
		A121_distance_config.start=0.18;
	}

	fcount=0;
	for(i=0;i<4;i++)
	{
		uf.c[i]=save_buf[temp++];
		if(uf.c[i]==0xff){fcount++;}
	}
	A121_distance_config.end=uf.f;
	if((A121_distance_config.end<0.1)||(A121_distance_config.end>20)||(fcount==4))
	{
		A121_distance_config.end=3.0;
	}

	if(A121_distance_config.start>A121_distance_config.end)
	{
		A121_distance_config.start=0.18;
		A121_distance_config.end=3.0;
	}

	A121_distance_config.step_length=((save_buf[temp++]<<8)&0xff00)+save_buf[temp++];
	if(A121_distance_config.step_length>1000)
	{
		A121_distance_config.step_length=1;
	}
	A121_distance_config.profile=save_buf[temp++];
	if(A121_distance_config.profile>5)
	{
		A121_distance_config.profile=3;
	}

	A121_distance_config.reflector_shape=save_buf[temp++];
	if(A121_distance_config.reflector_shape>1)
	{
		A121_distance_config.reflector_shape=0;
	}

	A121_distance_config.peak_sorting=save_buf[temp++];
	if(A121_distance_config.peak_sorting>1)
	{
		A121_distance_config.peak_sorting=1;
	}

	A121_distance_config.threshold_method=save_buf[temp++];
	if(A121_distance_config.threshold_method>3)
	{
		A121_distance_config.threshold_method=3;
	}
	A121_distance_config.num_frames_recorded=((save_buf[temp++]<<8)&0xff00)+save_buf[temp++];
	if(A121_distance_config.num_frames_recorded>511)
	{
		A121_distance_config.num_frames_recorded=100;
	}

	fcount=0;
	for(i=0;i<4;i++)
	{
		uf.c[i]=save_buf[temp++];
		if(uf.c[i]==0xff){fcount++;}
	}
	A121_distance_config.threshold_sensitivity=uf.f;
	if((A121_distance_config.threshold_sensitivity<0.1)||(A121_distance_config.threshold_sensitivity>100)||(fcount==4))
	{
		A121_distance_config.threshold_sensitivity=0.5;
	}

	fcount=0;
	for(i=0;i<4;i++)
	{
		uf.c[i]=save_buf[temp++];
		if(uf.c[i]==0xff){fcount++;}
	}
	A121_distance_config.signal_quality=uf.f;
	if((A121_distance_config.signal_quality<0.1)||(A121_distance_config.signal_quality>100)||(fcount==4))
	{
		A121_distance_config.signal_quality=15;
	}
	A121_distance_config.close_cancellation=save_buf[temp++];
	if(A121_distance_config.close_cancellation>1)
	{
		A121_distance_config.close_cancellation=0;
	}
/////
	A121_distance_config.hwaas=((save_buf[temp++]<<8)&0xff00)+save_buf[temp++];
	if(A121_distance_config.hwaas>511)
	{
		A121_distance_config.hwaas=16;
	}

	A121_distance_config.gain=save_buf[temp++];
	if(A121_distance_config.gain>23)
	{
		A121_distance_config.gain=16;
	}

	A121_distance_config.sweeps_per_frame=((save_buf[temp++]<<8)&0xff00)+save_buf[temp++];
	if(A121_distance_config.sweeps_per_frame>1000)
	{
		A121_distance_config.sweeps_per_frame=1;
	}

	for(i=0;i<4;i++)
	{
		uf.c[i]=save_buf[temp++];
	}
	A121_distance_config.sweep_rate=uf.f;
	if(A121_distance_config.sweep_rate>1000)
	{
		A121_distance_config.sweep_rate=0;
	}

	A121_distance_config.continuous_sweep_mode=save_buf[temp++];
	if(A121_distance_config.continuous_sweep_mode>1)
	{
		A121_distance_config.continuous_sweep_mode=0;
	}

	A121_distance_config.threshold=((save_buf[temp++]<<8)&0xff00)+save_buf[temp++];
	if(A121_distance_config.threshold>7000)
	{
		A121_distance_config.threshold=100;
	}

	fcount=0;
	for(i=0;i<4;i++)
	{
		uf.c[i]=save_buf[temp++];
		if(uf.c[i]==0xff){fcount++;}
	}
	A121_distance_config.frame_rate=uf.f;
	if((A121_distance_config.frame_rate>1000)||(fcount==4))
	{
		A121_distance_config.frame_rate=12;
	}

	A121_distance_config.frame_idle=save_buf[temp++];
	if(A121_distance_config.frame_idle>2)
	{
		A121_distance_config.frame_idle=1;
	}

	A121_distance_config.sweep_idle=save_buf[temp++];
	if(A121_distance_config.sweep_idle>2)
	{
		A121_distance_config.sweep_idle=1;
	}

	A121_distance_config.prf=save_buf[temp++];
	if(A121_distance_config.prf>5)
	{
		A121_distance_config.prf=4;
	}

	A121_distance_config.phase_enhancemen_flag=save_buf[temp++];
	if(A121_distance_config.phase_enhancemen_flag>1)
	{
		A121_distance_config.phase_enhancemen_flag=0;
	}

	A121_distance_config.double_buffering_flag=save_buf[temp++];
	if(A121_distance_config.double_buffering_flag>1)
	{
		A121_distance_config.double_buffering_flag=0;
	}

	A121_distance_config.data_output_time=((save_buf[temp++]<<8)&0xff00)+save_buf[temp++];
	if(A121_distance_config.data_output_time>5000)
	{
		A121_distance_config.data_output_time=100;
	}


	A121_distance_config.average=save_buf[temp++];
	if(A121_distance_config.average>100)
	{
		A121_distance_config.average=0;
	}

	A121_distance_config.scan_nub=save_buf[temp++];
	if(A121_distance_config.scan_nub>9)
	{
		A121_distance_config.scan_nub=1;
	}


	//校准参数
	acc_cal_result_save.use_cal_caching_flag=save_buf[temp++];
	if(acc_cal_result_save.use_cal_caching_flag>1)
	{
		acc_cal_result_save.use_cal_caching_flag=0;
	}
	acc_cal_result_save.cal_flag=save_buf[temp++];
	if(acc_cal_result_save.cal_flag>1)
	{
		acc_cal_result_save.cal_flag=0;
	}

	for(i=0;i<2;i++)
	{
		u16t.c[i]=save_buf[temp++];
	}
	acc_cal_result_save.close_buf_size=u16t.u16;
	if(acc_cal_result_save.close_buf_size>1500)
	{
		acc_cal_result_save.close_buf_size=1432;
	}

	acc_cal_result_save.temp_buf_size=save_buf[temp++];
	if(acc_cal_result_save.temp_buf_size>50)
	{
		acc_cal_result_save.temp_buf_size=48;
	}

	for(i=0;i<1500;i++)
	{
		acc_cal_result_save.close_buf[i]=save_buf[temp++];
	}
	for(j=0;j<50;j++)
	{
		for(i=0;i<4;i++)
		{
			u32t.c[i]=save_buf[temp++];
		}
		acc_cal_result_save.temp_buf[j]=u32t.u32;
	}

	A121_distance_config.run_status=1;
}


void presence_read_save_data(void)
{
	uint8_t i=0;
	uint8_t temp=0;
	uint8_t fcount=0;
	union uf_union  uf;

	syt_mode=save_buf[temp++];

	A121_presence_config.format=save_buf[temp++];
	if(A121_presence_config.format>1)
	{
		A121_presence_config.format=0;
	}
	A121_presence_config.ID=save_buf[temp++];
	if((A121_presence_config.ID>128)||(A121_presence_config.ID==0))
	{
		A121_presence_config.ID=1;
	}


	//	presence常规数据
	fcount=0;
	for(i=0;i<4;i++)
	{
		uf.c[i]=save_buf[temp++];
		if(uf.c[i]==0xff){fcount++;}
	}
	A121_presence_config.start=uf.f;
	if((A121_presence_config.start<0.001)||(A121_presence_config.start>20)||(fcount==4))
	{
		A121_presence_config.start=0.18;
	}

	fcount=0;
	for(i=0;i<4;i++)
	{
		uf.c[i]=save_buf[temp++];
		if(uf.c[i]==0xff){fcount++;}
	}
	A121_presence_config.end=uf.f;
	if((A121_presence_config.end<0.1)||(A121_presence_config.end>20)||(fcount==4))
	{
		A121_presence_config.end=1.2;
	}

	if(A121_presence_config.start>A121_presence_config.end)
	{
		A121_presence_config.start=0.18;
		A121_presence_config.end=1.2;
	}

	A121_presence_config.step_length=((save_buf[temp++]<<8)&0xff00)+save_buf[temp++];
	if(A121_presence_config.step_length>1000)
	{
		A121_presence_config.step_length=72;
	}
	A121_presence_config.profile=save_buf[temp++];
	if(A121_presence_config.profile>5)
	{
		A121_presence_config.profile=3;
	}
	A121_presence_config.frame_idle=save_buf[temp++];
	if(A121_presence_config.frame_idle>2)
	{
		A121_presence_config.frame_idle=1;
	}
	A121_presence_config.hwaas=((save_buf[temp++]<<8)&0xff00)+save_buf[temp++];
	if(A121_presence_config.hwaas>511)
	{
		A121_presence_config.hwaas=32;
	}
	A121_presence_config.sweeps_per_frame=((save_buf[temp++]<<8)&0xff00)+save_buf[temp++];
	if(A121_presence_config.sweeps_per_frame>1000)
	{
		A121_presence_config.sweeps_per_frame=16;
	}

	fcount=0;
	for(i=0;i<4;i++)
	{
		uf.c[i]=save_buf[temp++];
		if(uf.c[i]==0xff){fcount++;}
	}
	A121_presence_config.frame_rate=uf.f;
	if((A121_presence_config.frame_rate>1000)||(fcount==4))
	{
		A121_presence_config.frame_rate=12;
	}

	A121_presence_config.en_frame_rate=save_buf[temp++];
	if(A121_presence_config.en_frame_rate>1)
	{
		A121_presence_config.en_frame_rate=0;
	}
	A121_presence_config.en_reset_filters=save_buf[temp++];
	if(A121_presence_config.en_reset_filters>1)
	{
		A121_presence_config.en_reset_filters=0;
	}
	//	presence算法参数
	//帧内检测
	A121_presence_config.en_intra_detec_flag=save_buf[temp++];
	if(A121_presence_config.en_intra_detec_flag>1)
	{
		A121_presence_config.en_intra_detec_flag=1;
	}


	fcount=0;
	for(i=0;i<4;i++)
	{
		uf.c[i]=save_buf[temp++];
		if(uf.c[i]==0xff){fcount++;}
	}
	A121_presence_config.intra_threshold=uf.f;
	if((A121_presence_config.intra_threshold>5)||(fcount==4))
	{
		A121_presence_config.intra_threshold=1.2;
	}


	fcount=0;
	for(i=0;i<4;i++)
	{
		uf.c[i]=save_buf[temp++];
		if(uf.c[i]==0xff){fcount++;}
	}
	A121_presence_config.intra_frame_time=uf.f;
	if((A121_presence_config.intra_frame_time>1)||(fcount==4))
	{
		A121_presence_config.intra_frame_time=0.15;
	}


	fcount=0;
	for(i=0;i<4;i++)
	{
		uf.c[i]=save_buf[temp++];
		if(uf.c[i]==0xff){fcount++;}
	}
	A121_presence_config.intra_output_time=uf.f;
	if((A121_presence_config.intra_output_time>20)||(A121_presence_config.intra_output_time<0.1)||(fcount==4))
	{
		A121_presence_config.intra_output_time=0.3;
	}

	//帧间检测
	A121_presence_config.en_inter_detec_flag=save_buf[temp++];
	if(A121_presence_config.en_inter_detec_flag>1)
	{
		A121_presence_config.en_inter_detec_flag=1;
	}


	fcount=0;
	for(i=0;i<4;i++)
	{
		uf.c[i]=save_buf[temp++];
		if(uf.c[i]==0xff){fcount++;}
	}
	A121_presence_config.inter_threshold=uf.f;
	if((A121_presence_config.inter_threshold>5)||(fcount==4))
	{
		A121_presence_config.inter_threshold=1.2;
	}


	fcount=0;
	for(i=0;i<4;i++)
	{
		uf.c[i]=save_buf[temp++];
		if(uf.c[i]==0xff){fcount++;}
	}
	A121_presence_config.inter_deviation=uf.f;
	if((A121_presence_config.inter_deviation>20)||(A121_presence_config.inter_deviation<0.1)||(fcount==4))
	{
		A121_presence_config.inter_deviation=0.5;
	}


	fcount=0;
	for(i=0;i<4;i++)
	{
		uf.c[i]=save_buf[temp++];
		if(uf.c[i]==0xff){fcount++;}
	}
	A121_presence_config.inter_fast_cutoff=uf.f;
	if((A121_presence_config.inter_fast_cutoff>50)||(A121_presence_config.inter_fast_cutoff<1)||(fcount==4))
	{
		A121_presence_config.inter_fast_cutoff=6;
	}


	fcount=0;
	for(i=0;i<4;i++)
	{
		uf.c[i]=save_buf[temp++];
		if(uf.c[i]==0xff){fcount++;}
	}
	A121_presence_config.inter_slow_cutoff=uf.f;
	if((A121_presence_config.inter_slow_cutoff>1)||(A121_presence_config.inter_slow_cutoff<0.01)||(fcount==4))
	{
		A121_presence_config.inter_slow_cutoff=0.2;
	}

	fcount=0;
	for(i=0;i<4;i++)
	{
		uf.c[i]=save_buf[temp++];
		if(uf.c[i]==0xff){fcount++;}
	}
	A121_presence_config.inter_output_time=uf.f;
	if((A121_presence_config.inter_output_time>20)||(A121_presence_config.inter_output_time<0.01)||(fcount==4))
	{
		A121_presence_config.inter_output_time=2;
	}

	fcount=0;
	for(i=0;i<4;i++)
	{
		uf.c[i]=save_buf[temp++];
		if(uf.c[i]==0xff){fcount++;}
	}
	A121_presence_config.inter_frame_presence_timeout=uf.f;
	if((A121_presence_config.inter_frame_presence_timeout>1000)||(A121_presence_config.inter_frame_presence_timeout<0.01)||(fcount==4))
	{
		A121_presence_config.inter_frame_presence_timeout=3;
	}

	A121_presence_config.en_inter_phase_boost=save_buf[temp++];
	if(A121_presence_config.en_inter_phase_boost>1)
	{
		A121_presence_config.en_inter_phase_boost=1;
	}


	A121_presence_config.data_output_time=((save_buf[temp++]<<8)&0xff00)+save_buf[temp++];
	if(A121_presence_config.data_output_time>50000)
	{
		A121_presence_config.data_output_time=100;
	}
	A121_presence_config.run_status=1;
}


void breath_read_save_data(void)
{
	uint8_t temp=0;

	uint8_t flag=0;
	uint8_t i=0;
	union uf_union   uf;
	union u16_union  u16;


	syt_mode=save_buf[temp++];
	A121_breath_config.format=save_buf[temp++];
	A121_breath_config.ID=save_buf[temp++];

	for(i=0;i<4;i++){uf.c[i]=save_buf[temp++];}
	A121_breath_config.start=uf.f;

	for(i=0;i<4;i++){uf.c[i]=save_buf[temp++];}
	A121_breath_config.end=uf.f;


	for(i=0;i<2;i++){u16.c[i]=save_buf[temp++];}
	A121_breath_config.step_length=u16.u16;

	for(i=0;i<2;i++){u16.c[i]=save_buf[temp++];}
	A121_breath_config.num_dists_to_analyze=u16.u16;
	if((A121_breath_config.num_dists_to_analyze>64)||(A121_breath_config.num_dists_to_analyze<=0))
	{A121_breath_config.num_dists_to_analyze=20;}

	for(i=0;i<4;i++){uf.c[i]=save_buf[temp++];}
	A121_breath_config.distance_determination_duration=uf.f;


	A121_breath_config.use_presence_processor=save_buf[temp++];

	for(i=0;i<4;i++){uf.c[i]=save_buf[temp++];}
	A121_breath_config.lowest_breathing_rate=uf.f;


	for(i=0;i<4;i++){uf.c[i]=save_buf[temp++];}
	A121_breath_config.highest_breathing_rate=uf.f;

	for(i=0;i<4;i++){uf.c[i]=save_buf[temp++];}
	A121_breath_config.time_series_length_s=uf.f;

	for(i=0;i<4;i++){uf.c[i]=save_buf[temp++];}
	A121_breath_config.frame_rate=uf.f;


	for(i=0;i<2;i++){u16.c[i]=save_buf[temp++];}
	A121_breath_config.sweeps_per_frame=u16.u16;

	for(i=0;i<2;i++){u16.c[i]=save_buf[temp++];}
	A121_breath_config.hwaas=u16.u16;

	A121_breath_config.profile=save_buf[temp++];

		//////////////////////////////
	for(i=0;i<4;i++){uf.c[i]=save_buf[temp++];}
	A121_breath_config.Breath_presence.Intra_detection_threshold=uf.f;

	for(i=0;i<4;i++){uf.c[i]=save_buf[temp++];}
	A121_breath_config.Breath_presence.Intra_time_constant=uf.f;

	for(i=0;i<4;i++){uf.c[i]=save_buf[temp++];}
	A121_breath_config.Breath_presence.Intra_output_time_constant=uf.f;

	for(i=0;i<4;i++){uf.c[i]=save_buf[temp++];}
	A121_breath_config.Breath_presence.Inter_detection_threshold=uf.f;

	for(i=0;i<4;i++){uf.c[i]=save_buf[temp++];}
	A121_breath_config.Breath_presence.Inter_fast_cutoff_freq=uf.f;

	for(i=0;i<4;i++){uf.c[i]=save_buf[temp++];}
	A121_breath_config.Breath_presence.Inter_slow_cutoff_freq=uf.f;

	for(i=0;i<4;i++){uf.c[i]=save_buf[temp++];}
	A121_breath_config.Breath_presence.Inter_time_constant=uf.f;

	for(i=0;i<4;i++){uf.c[i]=save_buf[temp++];}
	A121_breath_config.Breath_presence.Inter_output_time_constant=uf.f;

}

void A121_app_read_data(void)
{
	STMFLASH_Read(BASE_ADDR, (uint32_t*)save_buf, SAVE_SIZE);
//	syt_mode=save_buf[0];
	if(syt_mode==0)
	{
		distance_read_save_data();
		printf("power on,run distance mode!\r\n");
		A121_distance_config.run_status=1;
	}
	else if(syt_mode==1)
	{
		presence_read_save_data();
		printf("power on,run presence mode!\r\n");
		A121_presence_config.run_status=1;
	}
	else
	{
		breath_read_save_data();
		printf("power on,run breath mode!\r\n");
		A121_breath_config.run_status=1;
	}

}


void  presence_UART_cmd(uint8_t *buf)
{
	uint8_t *str;
	uint8_t set_flag=0;
	uint8_t temp=0;

	if(strncmp(&buf[0], "AT+RST",6) == 0)
	{
		Reset_syt();
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+START",8) == 0)||(strncmp(&buf[0], "AT+ST",5) == 0))
	{
		A121_presence_config.run_status=1;
		printf("presence start...\r\n");
		set_flag=3;
	}
	else if((strncmp(&buf[0], "AT+STOP",7) == 0)||(strncmp(&buf[0], "AT+SP",5) == 0))
	{
		A121_presence_config.run_status=0;
		printf("presence stop\r\n");
		set_flag=3;
	}
	else if((strncmp(&buf[0], "AT+DOUT=",8) == 0)||(strncmp(&buf[0], "AT+DO=",6) == 0))
	{
		str=(strncmp(&buf[0], "AT+DOUT=",8) == 0)?&buf[8]:&buf[6];
		A121_presence_config.data_output_time=atoi(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+RANGE=",9) == 0)||(strncmp(&buf[0], "AT+RG=",6) == 0))
	{
		str=(strncmp(&buf[0], "AT+RANGE=",9) == 0)?&buf[9]:&buf[6];
		A121_presence_config.start=atof(str);
		str=strstr((uint8_t *)(str), ",");
		if ( str)
		{
			str+=1;
			A121_presence_config.end=atof(str);
		}
		set_flag=1;
	}

	else if((strncmp(&buf[0], "AT+STEP=",8) == 0)||(strncmp(&buf[0], "AT+SL=",6) == 0))
	{
		str=(strncmp(&buf[0], "AT+STEP=",8) == 0)?&buf[8]:&buf[6];
		A121_presence_config.step_length=atoi(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+PROF=",8) == 0)||(strncmp(&buf[0], "AT+PF=",6) == 0))
	{
		str=(strncmp(&buf[0], "AT+PROF=",8) == 0)?&buf[8]:&buf[6];
		A121_presence_config.profile=atoi(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+FIDLE=",9) == 0)||(strncmp(&buf[0], "AT+FI=",6) == 0))
	{
		str=(strncmp(&buf[0], "AT+FIDLE=",9) == 0)?&buf[9]:&buf[6];
		A121_presence_config.frame_idle=atoi(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+HWAAS=",9) == 0)||(strncmp(&buf[0], "AT+HW=",6) == 0))
	{
		str=(strncmp(&buf[0], "AT+HWAAS=",9) == 0)?&buf[9]:&buf[6];
		A121_presence_config.hwaas=atoi(str);
		set_flag=1;
	}
	else if(strncmp(&buf[0], "AT+SPF=",7) == 0)
	{
		A121_presence_config.sweeps_per_frame=atoi(&buf[7]);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+FRATE=",9) == 0)||(strncmp(&buf[0], "AT+FR=",6) == 0))
	{
		str=(strncmp(&buf[0], "AT+FRATE=",9) == 0)?&buf[9]:&buf[6];
		A121_presence_config.frame_rate=atof(str);
		set_flag=1;
	}
	///////////////////////////////////////////
	else if((strncmp(&buf[0], "AT+ENINTRA=",11) == 0)||(strncmp(&buf[0], "AT+EI=",6) == 0))
	{
		str=(strncmp(&buf[0], "AT+ENINTRA=",11) == 0)?&buf[11]:&buf[6];
		A121_presence_config.en_intra_detec_flag=atoi(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+INTRATHD=",12) == 0)||(strncmp(&buf[0], "AT+ITH=",7) == 0))
	{
		str=(strncmp(&buf[0], "AT+INTRATHD=",12) == 0)?&buf[12]:&buf[7];
		A121_presence_config.intra_threshold=atof(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+INTRATIME=",13) == 0)||(strncmp(&buf[0], "AT+ITM=",7) == 0))
	{
		str=(strncmp(&buf[0], "AT+INTRATIME=",13) == 0)?&buf[13]:&buf[7];
		A121_presence_config.intra_frame_time=atof(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+INTRAOUT=",12) == 0)||(strncmp(&buf[0], "AT+IO=",6) == 0))
	{
		str=(strncmp(&buf[0], "AT+INTRAOUT=",12) == 0)?&buf[12]:&buf[6];
		A121_presence_config.intra_output_time=atof(str);
		set_flag=1;
	}
	///////////////////////////////////////
	else if((strncmp(&buf[0], "AT+ENINTER=",11) == 0)||(strncmp(&buf[0], "AT+EE=",6) == 0))
	{
		str=(strncmp(&buf[0], "AT+ENINTER=",11) == 0)?&buf[11]:&buf[6];
		A121_presence_config.en_inter_detec_flag=atoi(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+INTERTHD=",12) == 0)||(strncmp(&buf[0], "AT+ETH=",7) == 0))
	{
		str=(strncmp(&buf[0], "AT+INTERTHD=",12) == 0)?&buf[12]:&buf[7];
		A121_presence_config.inter_threshold=atof(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+INTERDEV=",12) == 0)||(strncmp(&buf[0], "AT+IDV=",7) == 0))
	{
		str=(strncmp(&buf[0], "AT+INTERDEV=",12) == 0)?&buf[12]:&buf[7];
		A121_presence_config.inter_deviation=atof(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+INTERFAST=",13) == 0)||(strncmp(&buf[0], "AT+IFC=",7) == 0))
	{
		str=(strncmp(&buf[0], "AT+INTERFAST=",13) == 0)?&buf[13]:&buf[7];
		A121_presence_config.inter_fast_cutoff=atof(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+INTERSLOW=",13) == 0)||(strncmp(&buf[0], "AT+ISC=",7) == 0))
	{
		str=(strncmp(&buf[0], "AT+INTERSLOW=",13) == 0)?&buf[13]:&buf[7];
		A121_presence_config.inter_slow_cutoff=atof(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+INTEROUT=",12) == 0)||(strncmp(&buf[0], "AT+ITO=",7) == 0))
	{
		str=(strncmp(&buf[0], "AT+INTEROUT=",12) == 0)?&buf[12]:&buf[7];
		A121_presence_config.inter_output_time=atof(str);
		set_flag=1;
	}

	///////////////
	else if((strncmp(&buf[0], "AT+PRTIMEOUT=",13) == 0)||(strncmp(&buf[0], "AT+PTO=",7) == 0))
	{
		str=(strncmp(&buf[0], "AT+PRTIMEOUT=",13) == 0)?&buf[13]:&buf[7];
		A121_presence_config.inter_frame_presence_timeout=atof(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+ENPB=",8) == 0)||(strncmp(&buf[0], "AT+PB=",6) == 0))
	{
		str=(strncmp(&buf[0], "AT+ENPB=",8) == 0)?&buf[8]:&buf[6];
		A121_presence_config.en_inter_phase_boost=atoi(str);
		set_flag=1;
	}

	/////////////////
	else if((strncmp(&buf[0], "AT+CFG?",7) == 0)||(strncmp(&buf[0], "AT+CFG",6) == 0))//
	{
		printf("\r\nPresence configuration:\n");
		printf(" detection range :%.2f~%.2f m\n",A121_presence_config.start,A121_presence_config.end);
		printf("      step length=%5d,         profile=%d\n",A121_presence_config.step_length,A121_presence_config.profile);
		printf("       frame idle=%5d,           hwaas=%d\n",A121_presence_config.frame_idle,A121_presence_config.hwaas);
		printf(" sweeps per frame=%5d,      frame rate=%.2fHZ\n",A121_presence_config.sweeps_per_frame,A121_presence_config.frame_rate);
		printf("    en frame rate=%5d,en reset filters=%d\n",A121_presence_config.en_frame_rate,A121_presence_config.en_reset_filters);

		if(A121_presence_config.en_intra_detec_flag==1)
		{
		printf("\r\nEnable intra detector:\n");
		printf("        intra threshold  =%.2f\n",A121_presence_config.intra_threshold);
		printf("        intra frame time =%.2f\n",A121_presence_config.intra_frame_time);
		printf("        intra output time=%.2f\n",A121_presence_config.intra_threshold);
		}
		else
		{
			printf("Disable intra detector\n");
		}

		if(A121_presence_config.en_inter_detec_flag==1)
		{
		printf("\r\nEnable inter detector:\n");
		printf("        inter threshold  =%.2f\n",A121_presence_config.inter_threshold);
		printf("        inter deviation  =%.2f\n",A121_presence_config.inter_deviation);
		printf("        inter fast cutoff=%.2f\n",A121_presence_config.inter_fast_cutoff);
		printf("        inter slow cutoff=%.2f\n",A121_presence_config.inter_slow_cutoff);
		printf("        inter output time=%.2f\n",A121_presence_config.inter_output_time);

		}
		else
		{
			printf("Disable inter detector\n");
		}
		printf("inter_frame_presence_timeout=%.2f\n",A121_presence_config.inter_frame_presence_timeout);
		if(A121_presence_config.en_inter_phase_boost==1)
		{
			printf("Enable inter phase boost\n");
		}
		else
		{
			printf("Disable inter phase boost\r\n");
		}
		set_flag=3;
	}
	else if((strncmp(&buf[0], "AT+MODE=",8) == 0)||(strncmp(&buf[0], "AT+MD=",6) == 0))//
	{
		str=(strncmp(&buf[0], "AT+MODE=",8) == 0)?&buf[8]:&buf[6];
		syt_mode=atoi(str);
		if(syt_mode==0){printf("run distance mode\r\n");}
		else{printf("run presence mode\r\n");}
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+SETCFG=",10) == 0)||(strncmp(&buf[0], "AT+SC=",6) == 0))//
	{
		str=(strncmp(&buf[0], "AT+SETCFG=",10) == 0)?&buf[10]:&buf[6];
		temp=atoi(str);
		if(define_config_mode(temp)==1)
		{
			set_flag=1;
		}
	}

	if(set_flag==1)
	{
		printf("\r\nOK\r\n");
		A121_presence_config.save_flag=1;
	}
	else if(set_flag==0)
	{
		printf("\r\nERROR\r\n");
	}
}

void  dist_UART_cmd(uint8_t *buf)
{
	uint8_t *str;
	uint8_t set_flag=3;
	int16_t  data_tem;
	float  fdata_tem;
	uint16_t dc_dada;

	if(strncmp(&buf[0], "AT+RST",6) == 0)
	{
		Reset_syt();
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+START",8) == 0)||(strncmp(&buf[0], "AT+ST",5) == 0))
	{
		A121_distance_config.run_status=1;
		set_flag=3;
	}
	else if((strncmp(&buf[0], "AT+STOP",7) == 0)||(strncmp(&buf[0], "AT+SP",5) == 0))
	{
		A121_distance_config.run_status=0;
		set_flag=3;
	}
	else if((strncmp(&buf[0], "AT+PMODE=",9) == 0)||(strncmp(&buf[0], "AT+PM=",6) == 0))
	{
		str=(strncmp(&buf[0], "AT+PMODE=",9) == 0)?&buf[9]:&buf[6];
		A121_distance_config.printf_mode=atoi(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+DOUT=",8) == 0)||(strncmp(&buf[0], "AT+DO=",6) == 0))
	{
		str=(strncmp(&buf[0], "AT+DOUT=",8) == 0)?&buf[8]:&buf[6];
		A121_distance_config.data_output_time=atoi(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+RANGE=",9) == 0)||(strncmp(&buf[0], "AT+RG=",6) == 0))//AT+RANGE=0.18,3.0
	{
		str=(strncmp(&buf[0], "AT+RANGE=",9) == 0)?&buf[9]:&buf[6];
		fdata_tem=atof(str);
		if((fdata_tem>23)||(fdata_tem<0))
		{
			set_flag=0;
		}
		else
		{
			A121_distance_config.start=fdata_tem;
			str=strstr((uint8_t *)(str), ",");
			if ( str)
			{
				str+=1;
				fdata_tem=atof(str);
				if((fdata_tem<23.1)&&(fdata_tem>0)&&(fdata_tem>A121_distance_config.start))
				{
					A121_distance_config.end=fdata_tem;
					set_flag=1;
				}
			}

		}
		acc_cal_result_save.cal_flag=0;



	}
	else if((strncmp(&buf[0], "AT+STEP=",8) == 0)||(strncmp(&buf[0], "AT+SL=",6) == 0))//
	{
		str=(strncmp(&buf[0], "AT+STEP=",8) == 0)?&buf[8]:&buf[6];
		dc_dada=atoi(str);
		if(((dc_dada%24==0)&&(dc_dada<288))||(24%dc_dada==0))
		{
			A121_distance_config.step_length=dc_dada;
			acc_cal_result_save.cal_flag=0;
			set_flag=1;
		}
		else
		{
			set_flag=0;
		}
	}
	else if((strncmp(&buf[0], "AT+PROF=",8) == 0)||(strncmp(&buf[0], "AT+PF=",6) == 0))//
	{
		str=(strncmp(&buf[0], "AT+PROF=",8) == 0)?&buf[8]:&buf[6];
		dc_dada=atoi(str);
		if(dc_dada<6)
		{
			A121_distance_config.profile=dc_dada;
			acc_cal_result_save.cal_flag=0;
			set_flag=1;
		}
		else
		{
			set_flag=0;
		}
	}

	else if((strncmp(&buf[0], "AT+REFSHP=",10) == 0)||(strncmp(&buf[0], "AT+RS=",6) == 0))//
	{
		str=(strncmp(&buf[0], "AT+REFSHP=",10) == 0)?&buf[10]:&buf[6];
		dc_dada=atoi(str);
		if(dc_dada<2)
		{
			A121_distance_config.reflector_shape=dc_dada;
			acc_cal_result_save.cal_flag=0;
			set_flag=1;
		}
		else
		{
			set_flag=0;
		}
	}
	else if((strncmp(&buf[0], "AT+PKSORT=",10) == 0)||(strncmp(&buf[0], "AT+PS=",6) == 0))//
	{
		str=(strncmp(&buf[0], "AT+PKSORT=",10) == 0)?&buf[10]:&buf[6];
		dc_dada=atoi(str);

		if(dc_dada<2)
		{
			A121_distance_config.peak_sorting=dc_dada;
			acc_cal_result_save.cal_flag=0;
			set_flag=1;
		}
		else
		{
			set_flag=0;
		}

	}
	else if((strncmp(&buf[0], "AT+THMET=",9) == 0)||(strncmp(&buf[0], "AT+TM=",6) == 0))//
	{
		str=(strncmp(&buf[0], "AT+THMET=",9) == 0)?&buf[9]:&buf[6];
		A121_distance_config.threshold_method=atoi(str);
		acc_cal_result_save.cal_flag=0;
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+NFRAME=",10) == 0)||(strncmp(&buf[0], "AT+NF=",6) == 0))//
	{
		str=(strncmp(&buf[0], "AT+NFRAME=",10) == 0)?&buf[10]:&buf[6];
		A121_distance_config.num_frames_recorded=atoi(str);
		acc_cal_result_save.cal_flag=0;
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+THSEN=",9) == 0)||(strncmp(&buf[0], "AT+TS=",6) == 0))//
	{
		str=(strncmp(&buf[0], "AT+THSEN=",9) == 0)?&buf[9]:&buf[6];
		fdata_tem=atof(str);
		if((fdata_tem>1)||(fdata_tem<0))
		{
			set_flag=0;
		}
		else
		{
			set_flag=1;
			A121_distance_config.threshold_sensitivity=fdata_tem;
			acc_cal_result_save.cal_flag=0;
		}
	}
	else if((strncmp(&buf[0], "AT+SIGQ=",8) == 0)||(strncmp(&buf[0], "AT+SQ=",6) == 0))//
	{
		str=(strncmp(&buf[0], "AT+SIGQ=",8) == 0)?&buf[8]:&buf[6];
		data_tem=atoi(str);
		if((data_tem>35)||(data_tem<-10))
		{
			set_flag=0;
		}
		else
		{
			A121_distance_config.signal_quality=data_tem;
			acc_cal_result_save.cal_flag=0;
			set_flag=1;
		}

	}
	else if((strncmp(&buf[0], "AT+CLOSECAN=",12) == 0)||(strncmp(&buf[0], "AT+CC=",6) == 0))//
	{
		str=(strncmp(&buf[0], "AT+CLOSECAN=",12) == 0)?&buf[12]:&buf[6];
		dc_dada=atoi(str);

		if(dc_dada<2)
		{
			A121_distance_config.close_cancellation=dc_dada;
			acc_cal_result_save.cal_flag=0;
			set_flag=1;
		}
		else
		{
			set_flag=0;
		}
	}

	///////////////////////////


	else if((strncmp(&buf[0], "AT+HWAAS=",9) == 0)||(strncmp(&buf[0], "AT+HW=",6) == 0))//
	{
		str=(strncmp(&buf[0], "AT+HWAAS=",9) == 0)?&buf[9]:&buf[6];
		A121_distance_config.hwaas=atoi(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+GAIN=",8) == 0)||(strncmp(&buf[0], "AT+GN=",6) == 0))//
	{
		str=(strncmp(&buf[0], "AT+GAIN=",8) == 0)?&buf[8]:&buf[6];
		A121_distance_config.gain=atoi(str);
		set_flag=1;
	}
	else if(strncmp(&buf[0], "AT+SPF=",7) == 0)//
	{
		A121_distance_config.sweeps_per_frame=atoi(&buf[7]);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+SWRATE=",10) == 0)||(strncmp(&buf[0], "AT+SWR=",7) == 0))//
	{
		str=(strncmp(&buf[0], "AT+SWRATE=",10) == 0)?&buf[10]:&buf[7];
		A121_distance_config.sweep_rate=atof(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+CONTMD=",10) == 0)||(strncmp(&buf[0], "AT+CM=",6) == 0))//
	{
		str=(strncmp(&buf[0], "AT+CONTMD=",10) == 0)?&buf[10]:&buf[6];
		A121_distance_config.continuous_sweep_mode=atoi(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+FRATE=",9) == 0)||(strncmp(&buf[0], "AT+FR=",6) == 0))//
	{
		str=(strncmp(&buf[0], "AT+FRATE=",9) == 0)?&buf[9]:&buf[6];
		A121_distance_config.frame_rate=atof(str);
		set_flag=1;
	}
	else if(strncmp(&buf[0], "AT+TH=",6) == 0)//
	{
		A121_distance_config.threshold=atoi(&buf[6]);
		set_flag=1;
	}

	else if((strncmp(&buf[0], "AT+FIDLE=",9) == 0)||(strncmp(&buf[0], "AT+FI=",6) == 0))//
	{
		str=(strncmp(&buf[0], "AT+FIDLE=",9) == 0)?&buf[9]:&buf[6];
		A121_distance_config.frame_idle=atoi(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+SIDLE=",9) == 0)||(strncmp(&buf[0], "AT+SI=",6) == 0))//
	{
		str=(strncmp(&buf[0], "AT+SIDLE=",9) == 0)?&buf[9]:&buf[6];
		A121_distance_config.sweep_idle=atoi(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+PHENH=",9) == 0)||(strncmp(&buf[0], "AT+PE=",6) == 0))//
	{
		str=(strncmp(&buf[0], "AT+PHENH=",9) == 0)?&buf[9]:&buf[6];
		A121_distance_config.phase_enhancemen_flag=atoi(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+DBLBUF=",10) == 0)||(strncmp(&buf[0], "AT+DB=",6) == 0))//
	{
		str=(strncmp(&buf[0], "AT+DBLBUF=",10) == 0)?&buf[10]:&buf[6];
		A121_distance_config.double_buffering_flag=atoi(str);
		set_flag=1;
	}
	else if(strncmp(&buf[0], "AT+AVG=",7) == 0)//
	{

		dc_dada=atoi(&buf[7]);

		if(dc_dada<100)
		{
			A121_distance_config.average=dc_dada;
			set_flag=1;
		}
		else
		{
			set_flag=0;
		}
	}
	else if((strncmp(&buf[0], "AT+SCANNB=",10) == 0)||(strncmp(&buf[0], "AT+SN=",6) == 0))//
	{
		str=(strncmp(&buf[0], "AT+SCANNB=",10) == 0)?&buf[10]:&buf[6];
		dc_dada=atoi(str);

		if(dc_dada<11)
		{
			A121_distance_config.scan_nub=dc_dada;
			set_flag=1;
		}
		else
		{
			set_flag=0;
		}
	}

//掉电保存
	else if((strncmp(&buf[0], "AT+CALCACHE=",12) == 0)||(strncmp(&buf[0], "AT+CAL=",7) == 0))//
	{
		str=(strncmp(&buf[0], "AT+CALCACHE=",12) == 0)?&buf[12]:&buf[7];
		dc_dada=atoi(str);
		if(dc_dada<2)
		{
			if(acc_cal_result_save.use_cal_caching_flag!=dc_dada)
			{
				acc_cal_result_save.cal_flag=0;
			}
			acc_cal_result_save.use_cal_caching_flag=dc_dada;
			set_flag=1;
		}
		else
		{
			set_flag=0;
		}
	}
	else if(strncmp(&buf[0], "AT+CALRST",9) == 0)//
	{
		acc_cal_result_save.cal_flag=0;
		acc_cal_result_save.acc_recal=1;
		set_flag=1;
	}


	/////////////////
	else if((strncmp(&buf[0], "AT+CFG?",7) == 0)||(strncmp(&buf[0], "AT+CFG",6) == 0))//
	{
		printf_distance_config();

		set_flag=3;
	}
	else if((strncmp(&buf[0], "AT+CFGRST",9) == 0)||(strncmp(&buf[0], "AT+CRST",7) == 0))//
	{
		reset_distance_setting();
		printf_distance_config();
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+MODE=",8) == 0)||(strncmp(&buf[0], "AT+MD=",6) == 0))//
	{
		str=(strncmp(&buf[0], "AT+MODE=",8) == 0)?&buf[8]:&buf[6];
		syt_mode=atoi(str);
		if(syt_mode==0){printf("run distance mode\r\n");}
		else{printf("run presence mode\r\n");}
		set_flag=1;
	}

	if(set_flag==1 || set_flag==3)
	{
		printf("\r\nOK\r\n");
		A121_distance_config.save_flag=1;
	}
	else if(set_flag==0)
	{
		printf("\r\nERROR\r\n");
	}

}

//void  acc_dist_UART_cmd(uint8_t *buf)
//{
//	uint8_t *str;
//	uint8_t set_flag=0;
//	if(strncmp(&buf[0], "syt rest",8) == 0)
//	{
//		Reset_syt();
//	}
//	else if(strncmp(&buf[0], "Distance Start",14) == 0)
//	{
//		A121_distance_config.run_status=1;
//	}
//	else if(strncmp(&buf[0], "Distance Stop",13) == 0)
//	{
//		A121_distance_config.run_status=0;
//	}
//	else if(strncmp(&buf[0], "Set data output time=",21) == 0)
//	{
//		A121_distance_config.data_output_time=atoi(&buf[21]);
//		set_flag=1;
//	}
//	else if(strncmp(&buf[0], "Set start=",10) == 0)//Set start=300,end=300
//	{
//		A121_distance_config.start=atof(&buf[10]);
//		str=strstr((uint8_t *)(&buf[11]), "end=");
//		if ( str)
//		{
//			str+=4;
//			A121_distance_config.end=atof(str);
//		}
//		set_flag=1;
//	}
//	else if(strncmp(&buf[0], "Set step length=",16) == 0)//
//	{
//		A121_distance_config.step_length=atoi(&buf[16]);
//		set_flag=1;
//	}
//	else if(strncmp(&buf[0], "Set profile=",12) == 0)//
//	{
//		A121_distance_config.profile=atoi(&buf[12]);
//		set_flag=1;
//	}
//	else if(strncmp(&buf[0], "Set hwaas=",10) == 0)//
//	{
//		A121_distance_config.hwaas=atoi(&buf[10]);
//		set_flag=1;
//	}
//	else if(strncmp(&buf[0], "Set gain=",9) == 0)//
//	{
//		A121_distance_config.gain=atoi(&buf[9]);
//		set_flag=1;
//	}
//	else if(strncmp(&buf[0], "Set sweeps per frame=",21) == 0)//
//	{
//		A121_distance_config.sweeps_per_frame=atoi(&buf[21]);
//		set_flag=1;
//	}
//	else if(strncmp(&buf[0], "Set sweep rate=",15) == 0)//
//	{
//		A121_distance_config.sweep_rate=atof(&buf[15]);
//		set_flag=1;
//	}
//	else if(strncmp(&buf[0], "Set continuous mode=",20) == 0)//
//	{
//		A121_distance_config.continuous_sweep_mode=atoi(&buf[20]);
//		set_flag=1;
//	}
//	else if(strncmp(&buf[0], "Set frame rate=",15) == 0)//
//	{
//		A121_distance_config.frame_rate=atof(&buf[15]);
//		set_flag=1;
//	}
//	else if(strncmp(&buf[0], "Set threshold=",14) == 0)//
//	{
//		A121_distance_config.threshold=atoi(&buf[14]);
//		set_flag=1;
//	}
//
//	else if(strncmp(&buf[0], "Set frame idle=",15) == 0)//
//	{
//		A121_distance_config.frame_idle=atoi(&buf[15]);
//		set_flag=1;
//	}
//	else if(strncmp(&buf[0], "Set sweep idle=",15) == 0)//
//	{
//		A121_distance_config.sweep_idle=atoi(&buf[15]);
//		set_flag=1;
//	}
//	else if(strncmp(&buf[0], "Set phase enhancemen=",21) == 0)//
//	{
//		A121_distance_config.phase_enhancemen_flag=atoi(&buf[21]);
//		set_flag=1;
//	}
//	else if(strncmp(&buf[0], "Set double buffering=",21) == 0)//
//	{
//		A121_distance_config.double_buffering_flag=atoi(&buf[21]);
//		set_flag=1;
//	}
//
//
//	/////////////////
//	else if(strncmp(&buf[0], "Get Configuration",17) == 0)//
//	{
//		printf("Distance configuration:\n");
//		printf("detection range :%.2f~%.2f m\n",A121_distance_config.start,A121_distance_config.end);
//		printf("step length     =%5d,          profile=%d\n",A121_distance_config.step_length,A121_distance_config.profile);
//		printf("hwaas           =%5d,             gain=%d\n",A121_distance_config.hwaas,A121_distance_config.gain);
//
//		printf("sweeps per frame=%5d,       sweep rate=%.2fHZ,sweep mode=%d\n",A121_distance_config.sweeps_per_frame,A121_distance_config.sweep_rate,A121_distance_config.continuous_sweep_mode);
//
//		printf("threshold       =%5d,       frame rate=%.2fHZ\n",A121_distance_config.threshold,A121_distance_config.frame_rate);
//
//		printf("frame idle      =%5d,       sweep idle=%dHZ\n",A121_distance_config.frame_idle,A121_distance_config.sweep_idle);
//
//		printf("phase enhancemen=%5d, double bufferine=%dHZ\n",A121_distance_config.phase_enhancemen_flag,A121_distance_config.double_buffering_flag);
//
//	}
//	else if(strncmp(&buf[0], "Rest Configuration",18) == 0)//
//	{
//		reset_dist_set();
//		printf("Distance configuration:\n");
//		printf("detection range :%.2f~%.2f m\n",A121_distance_config.start,A121_distance_config.end);
//		printf("step length     =%5d,          profile=%d\n",A121_distance_config.step_length,A121_distance_config.profile);
//		printf("hwaas           =%5d,             gain=%d\n",A121_distance_config.hwaas,A121_distance_config.gain);
//
//		printf("sweeps per frame=%5d,       sweep rate=%.2fHZ,sweep mode=%d\n",A121_distance_config.sweeps_per_frame,A121_distance_config.sweep_rate,A121_distance_config.continuous_sweep_mode);
//
//		printf("threshold       =%5d,       frame rate=%.2fHZ\n",A121_distance_config.threshold,A121_distance_config.frame_rate);
//
//		printf("frame idle      =%5d,       sweep idle=%d\n",A121_distance_config.frame_idle,A121_distance_config.sweep_idle);
//
//		printf("phase enhancemen=%5d, double bufferine=%d\n",A121_distance_config.phase_enhancemen_flag,A121_distance_config.double_buffering_flag);
//
//	}
//	else if(strncmp(&buf[0], "set mode=",9) == 0)//
//	{
//		syt_mode=atoi(&buf[9]);
//		if(syt_mode==0){printf("run distance mode\r\n");}
//		else{printf("run presence mode\r\n");}
//		set_flag=1;
//	}
//	else
//	{
//		printf("Command error\r\n");
//	}
//	if(set_flag==1)
//	{
//		printf("\nSET OK\r\n");
//		A121_distance_config.save_flag=1;
//	}
//}

void printf_breath_config(void)
{
	printf("\r\n      Breath configuration:\n");
	printf("               detection range:%.2f~%.2f m\n",A121_breath_config.start,A121_breath_config.end);
	printf("                    step_length=%d\n",A121_breath_config.step_length);
	printf("           num_dists_to_analyze=%d\n",A121_breath_config.num_dists_to_analyze);
	printf("distance_determination_duration=%.2f\n",A121_breath_config.distance_determination_duration);
	printf("         use_presence_processor=%d\n",A121_breath_config.use_presence_processor);

	printf("          lowest_breathing_rate=%.2f\n",A121_breath_config.lowest_breathing_rate);
	printf("         highest_breathing_rate=%.2f\n",A121_breath_config.highest_breathing_rate);
	printf("           time_series_length_s=%.2f\n",A121_breath_config.time_series_length_s);
	printf("                     frame_rate=%.2f\n",A121_breath_config.frame_rate);
	printf("               sweeps_per_frame=%d\n",A121_breath_config.sweeps_per_frame);

	printf("                          hwaas=%d\n",A121_breath_config.hwaas);
	printf("                        profile=%d\n",A121_breath_config.profile);


	printf("      Inter_detection_threshold=%.2f\n",A121_breath_config.Breath_presence.Intra_time_constant);
	printf("            Intra_time_constant=%.2f\n",A121_breath_config.Breath_presence.Intra_time_constant);
	printf("     Intra_output_time_constant=%.2f\n",A121_breath_config.Breath_presence.Intra_output_time_constant);

	printf("      Inter_detection_threshold=%.2f\n",A121_breath_config.Breath_presence.Inter_detection_threshold);
	printf("         Inter_fast_cutoff_freq=%.2f\n",A121_breath_config.Breath_presence.Inter_fast_cutoff_freq);
	printf("         Inter_slow_cutoff_freq=%.2f\n",A121_breath_config.Breath_presence.Inter_slow_cutoff_freq);

	printf("            Inter_time_constant=%.2f\n",A121_breath_config.Breath_presence.Inter_time_constant);
	printf("     Inter_output_time_constant=%.2f\n",A121_breath_config.Breath_presence.Inter_output_time_constant);
}

void  breath_UART_cmd(uint8_t *buf)
{
	uint8_t *str;
	uint8_t set_flag=0;
	uint8_t temp=0;

	if(strncmp(&buf[0], "AT+RST",6) == 0)
	{
		Reset_syt();
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+START",8) == 0)||(strncmp(&buf[0], "AT+ST",5) == 0))
	{
		A121_breath_config.run_status=1;
		printf("Breath start...\r\n");
		set_flag=3;
	}
	else if((strncmp(&buf[0], "AT+STOP",7) == 0)||(strncmp(&buf[0], "AT+SP",5) == 0))
	{
		A121_breath_config.run_status=0;
		printf("breath stop\r\n");
		set_flag=3;
	}

	else if((strncmp(&buf[0], "AT+RANGE=",9) == 0)||(strncmp(&buf[0], "AT+RG=",6) == 0))
	{
		str=(strncmp(&buf[0], "AT+RANGE=",9) == 0)?&buf[9]:&buf[6];
		A121_breath_config.start=atof(str);
		str=strstr((uint8_t *)(str), ",");
		if ( str)
		{
			str+=1;
			A121_breath_config.end=atof(str);
		}
		set_flag=1;
	}

	else if((strncmp(&buf[0], "AT+BRLINE=",10) == 0)||(strncmp(&buf[0], "AT+BL=",6) == 0))
	{
		str=(strncmp(&buf[0], "AT+BRLINE=",10) == 0)?&buf[10]:&buf[6];
		A121_breath_config.line=atoi(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+STEP=",8) == 0)||(strncmp(&buf[0], "AT+SL=",6) == 0))
	{
		str=(strncmp(&buf[0], "AT+STEP=",8) == 0)?&buf[8]:&buf[6];
		A121_breath_config.step_length=atoi(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+NUMDIST=",11) == 0)||(strncmp(&buf[0], "AT+ND=",6) == 0))
	{
		str=(strncmp(&buf[0], "AT+NUMDIST=",11) == 0)?&buf[11]:&buf[6];
		A121_breath_config.num_dists_to_analyze=atoi(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+DISTDUR=",11) == 0)||(strncmp(&buf[0], "AT+DD=",6) == 0))
	{
		str=(strncmp(&buf[0], "AT+DISTDUR=",11) == 0)?&buf[11]:&buf[6];
		A121_breath_config.distance_determination_duration=atof(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+USEPR=",9) == 0)||(strncmp(&buf[0], "AT+UP=",6) == 0))
	{
		str=(strncmp(&buf[0], "AT+USEPR=",9) == 0)?&buf[9]:&buf[6];
		A121_breath_config.use_presence_processor=atoi(str);
		set_flag=1;
	}

	else if((strncmp(&buf[0], "AT+LOWBR=",9) == 0)||(strncmp(&buf[0], "AT+LB=",6) == 0))
	{
		str=(strncmp(&buf[0], "AT+LOWBR=",9) == 0)?&buf[9]:&buf[6];
		A121_breath_config.lowest_breathing_rate=atof(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+HIGHBR=",10) == 0)||(strncmp(&buf[0], "AT+HB=",6) == 0))
	{
		str=(strncmp(&buf[0], "AT+HIGHBR=",10) == 0)?&buf[10]:&buf[6];
		A121_breath_config.highest_breathing_rate=atof(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+TIMELEN=",11) == 0)||(strncmp(&buf[0], "AT+TL=",6) == 0))
	{
		str=(strncmp(&buf[0], "AT+TIMELEN=",11) == 0)?&buf[11]:&buf[6];
		A121_breath_config.time_series_length_s=atof(str);
		set_flag=1;
	}



	else if((strncmp(&buf[0], "AT+FRATE=",9) == 0)||(strncmp(&buf[0], "AT+FR=",6) == 0))
	{
		str=(strncmp(&buf[0], "AT+FRATE=",9) == 0)?&buf[9]:&buf[6];
		A121_breath_config.frame_rate=atof(str);
		set_flag=1;
	}
	else if(strncmp(&buf[0], "AT+SPF=",7) == 0)
	{
		A121_breath_config.sweeps_per_frame=atoi(&buf[7]);
		set_flag=1;
	}

	else if((strncmp(&buf[0], "AT+HWAAS=",9) == 0)||(strncmp(&buf[0], "AT+HW=",6) == 0))
	{
		str=(strncmp(&buf[0], "AT+HWAAS=",9) == 0)?&buf[9]:&buf[6];
		A121_breath_config.hwaas=atoi(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+PROF=",8) == 0)||(strncmp(&buf[0], "AT+PF=",6) == 0))
	{
		str=(strncmp(&buf[0], "AT+PROF=",8) == 0)?&buf[8]:&buf[6];
		A121_breath_config.profile=atoi(str);
		set_flag=1;
	}


	///////////////////////////////////////////
	else if((strncmp(&buf[0], "AT+BRAINTD=",11) == 0)||(strncmp(&buf[0], "AT+BID=",7) == 0))
	{
		str=(strncmp(&buf[0], "AT+BRAINTD=",11) == 0)?&buf[11]:&buf[7];
		A121_breath_config.Breath_presence.Intra_detection_threshold=atof(str);
		set_flag=1;
	}

	else if((strncmp(&buf[0], "AT+BRAITC=",10) == 0)||(strncmp(&buf[0], "AT+BIC=",7) == 0))
	{
		str=(strncmp(&buf[0], "AT+BRAITC=",10) == 0)?&buf[10]:&buf[7];
		A121_breath_config.Breath_presence.Intra_time_constant=atof(str);
		set_flag=1;
	}

	else if((strncmp(&buf[0], "AT+BRAIOT=",10) == 0)||(strncmp(&buf[0], "AT+BIO=",7) == 0))
	{
		str=(strncmp(&buf[0], "AT+BRAIOT=",10) == 0)?&buf[10]:&buf[7];
		A121_breath_config.Breath_presence.Intra_output_time_constant=atof(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+BRAETD=",10) == 0)||(strncmp(&buf[0], "AT+BED=",7) == 0))
	{
		str=(strncmp(&buf[0], "AT+BRAETD=",10) == 0)?&buf[10]:&buf[7];
		A121_breath_config.Breath_presence.Inter_detection_threshold=atof(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+BRAEFC=",10) == 0)||(strncmp(&buf[0], "AT+BFC=",7) == 0))
	{
		str=(strncmp(&buf[0], "AT+BRAEFC=",10) == 0)?&buf[10]:&buf[7];
		A121_breath_config.Breath_presence.Inter_fast_cutoff_freq=atof(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+BRAESC=",10) == 0)||(strncmp(&buf[0], "AT+BSC=",7) == 0))
	{
		str=(strncmp(&buf[0], "AT+BRAESC=",10) == 0)?&buf[10]:&buf[7];
		A121_breath_config.Breath_presence.Inter_slow_cutoff_freq=atof(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+BRAETC=",10) == 0)||(strncmp(&buf[0], "AT+BEC=",7) == 0))
	{
		str=(strncmp(&buf[0], "AT+BRAETC=",10) == 0)?&buf[10]:&buf[7];
		A121_breath_config.Breath_presence.Inter_time_constant=atof(str);
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+BRAEOT=",10) == 0)||(strncmp(&buf[0], "AT+BEO=",7) == 0))
	{
		str=(strncmp(&buf[0], "AT+BRAEOT=",10) == 0)?&buf[10]:&buf[7];
		A121_breath_config.Breath_presence.Inter_output_time_constant=atof(str);
		set_flag=1;
	}


	/////////////////
	else if((strncmp(&buf[0], "AT+CFG?",7) == 0)||(strncmp(&buf[0], "AT+CFG",6) == 0))//
	{
		syt_mode=3;
		A121_app_read_data();
		printf_breath_config();
		set_flag=3;
	}
	else if((strncmp(&buf[0], "AT+MODE=",8) == 0)||(strncmp(&buf[0], "AT+MD=",6) == 0))//
	{
		str=(strncmp(&buf[0], "AT+MODE=",8) == 0)?&buf[8]:&buf[6];
		syt_mode=atoi(str);
		if(syt_mode==0){printf("run distance mode\r\n");}
		else if(syt_mode==1){printf("run presence mode\r\n");}
		else
		{
			printf("run breathing mode\r\n");
		}
		set_flag=1;
	}
	else if((strncmp(&buf[0], "AT+CFGRST",9) == 0)||(strncmp(&buf[0], "AT+CRST",7) == 0))//
	{
		reset_breath_config();
		printf_breath_config();
		set_flag=1;
		A121_breath_config.save_flag=1;
	}

	if(set_flag==1)
	{
		printf("\r\nOK\r\n");
		A121_breath_config.save_flag=1;
	}
	else if(set_flag==0)
	{
		printf("\r\nERROR\r\n");
	}
}


void A121_UART_cmd(void)
{
	if(syt_mode==0)
	{
		dist_UART_cmd(USART1_RX_BUF);
	}
	else if(syt_mode==1)
	{
		presence_UART_cmd(USART1_RX_BUF);
	}
	else
	{
		breath_UART_cmd(USART1_RX_BUF);
	}

}

